//
//  HMOriginalView.swift
//  Weibo
//
//  Created by 李学林 on 15/12/10.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit
import SnapKit
class HMOriginalView: UIView {
    
    // 判断底部约束的变量
    var bottomConstraint: Constraint?
    
    var statusViewModel: HMStatusViewModel? {
        didSet{
            // 这个方法中就可以给下面的子空间们赋值了
            // 设置头像
            iconImageView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            // 设置昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            // 设置刚刚
            timeLabel.text = statusViewModel?.creatAtStr
            // 来源
            sourceLabel.text = statusViewModel?.sourceStr
            // 设置vip
            vipImageView.image = statusViewModel?.vipImage
            // 设置认证
            verticalImageView.image = statusViewModel?.vertical
            
            // 设置原创微博内容
            contentLabel.text = statusViewModel?.status?.text
            
            
            // 设置原创微博的配图
            // 更新约束之前先移除
            bottomConstraint?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0{
                //----设置数据----重点
                pictureView.pic_urls = pic_urls
                pictureView.hidden = false
                snp_updateConstraints(closure: { (make) -> Void in
                   self.bottomConstraint = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
                })
            }else{

                pictureView.hidden = true

                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint =  make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
                })
                
                
            }

        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        //backgroundColor = RandomColor()
        
        // 添加子控件
        // 添加头像控件
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verticalImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        // 头像
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HMStatusCellMargin)
            make.top.equalTo(self).offset(HMStatusCellMargin)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        // 昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView)
            make.leading.equalTo(iconImageView.snp_trailing).offset(HMStatusCellMargin)
        }
        // vip图标
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
            
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImageView)
            make.leading.equalTo(iconImageView.snp_trailing).offset(HMStatusCellMargin)
        }
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(HMStatusCellMargin)
        }
        // 认证
        verticalImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView.snp_trailing)
            make.centerY.equalTo(iconImageView.snp_bottom)
        }
        // 原创微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconImageView)
            // make.trailing.equalTo(self).offset(-HMStatusCellMargin)
            make.top.equalTo(iconImageView.snp_bottom).offset(HMStatusCellMargin)
        }
        
        // 配图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        // 关键约束
        snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
        }
        
        
        
    }
    
    
    // 懒加载控件
    // 头像控件
    private lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    // 昵称label
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textSize: 14)
    // 时间label
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), textSize: 10)
    // 来自
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), textSize: 10)
    // 会员图标
    private lazy var vipImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 认证图标
    private lazy var verticalImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 微博内容
    private lazy  var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textSize: 15,maxWidth: SCREENW -  2 * HMStatusCellMargin)
    
    // 原微博配图
    private lazy var pictureView :HMPictureView = {
        
        let picture = HMPictureView()
        picture.backgroundColor = self.backgroundColor
        return picture
        
    }()
    
}
